<?php
iconv_set_encoding("internal_encoding", "UTF-8");
require('Log.php');
require('HttpClient.php');
require('Model.php');

class Client 
{
	private $httpClient;

	private $model;

	private $config;
	
	public $LandOK=0;
	public $SubLandOK=0;

	function __construct($config)
	{
		$this->config = $config;
		// $this->config = require('Config.php');
		$this->httpClient = new HttpClient($config);
		$this->model = new Model($config);
	}

	public function getModel()
	{
		if(!isset($this->model)){
			$this->model = new Model($this->config);
		}
		return $this->model;
	}

	public function getHttpClient()
	{
		if(!isset($this->httpClient)){
			$this->httpClient = new HttpClient($this->config);
		}
		return $this->httpClient;
	}

	public function insertLandInfo()
	{
		List($landInfo_ID,$LandInfo) = $this->getModel()->getLandInfo();	
		//echo 'LandInfo11:';
		//var_dump($LandInfo);
		if($LandInfo) {			
			$LandInfo = json_encode($LandInfo);
			$LandInfo = iconv("GBK//IGNORE", "UTF-8", $LandInfo);			
			Log::logger('LandInfo: ' . $LandInfo);
			$LandInfo = base64_encode($LandInfo);
			$res = $this->httpClient->post($this->config['insertLandInfo'], $LandInfo);
			Log::logger('LandInfo Response: ' . $res);
			echo 'response:' . $res . "</br>";		
			
			if($res && $res = json_decode($res, true)){
				
				if(0 == $res['code'] && $res['result']['id']){					
				
					 $this->getModel()->setLandId($res['result']['id'], $landInfo_ID);
				}
			}		
		} else {
			$this->LandOK=6;
			Log::logger('All LandInfo Uploaded!');
			echo 'LandOK:'.$this->LandOK."<br>";
		}
	}


	
	public function insertMultiBlockInfo()
	{
		$multiBlockInfo = $this->getModel()->getMultiBlockInfo();
		if (isset($multiBlockInfo))
		{
		foreach ($multiBlockInfo as $key => $value) {
			$blockInfo = json_encode($value);
			$blockInfo = iconv("GBK//IGNORE", "UTF-8", $blockInfo);
			Log::logger('BlockInfo: ' . $blockInfo);
			$blockInfo = base64_encode($blockInfo);
			$res = $this->httpClient->post($this->config['insertBlockInfo'], $blockInfo);
			Log::logger('BlockInfo Response: ' . $res);
			if($res && $res = json_decode($res, true)){
				if(0 == $res['code'] && $res['result']['id']){
					$this->getModel()->setSubLandID($res['result']['id'], $value['ID']);
				}
			}	
			$this->getModel()->setSubLandID($key, $value['ID']);
		}
		
		}
		else{
			
			Log::logger('All BlockInfo Uploaded!');
			$this->SubLandOK=6;
			echo 'SubLandOK:'.$this->SubLandOK."<br>";
		}
	}


	public function insertMultiBlockEnvInfo()
	{
		$multiBlockEnvInfo = $this->getModel()->getMultiBlockEnvInfo();
		//echo 'multiBlockEnvInfo:';
		//var_dump($multiBlockEnvInfo);
		$multiBlockEnvInfo = json_encode($multiBlockEnvInfo);
		$multiBlockEnvInfo = iconv("GBK//IGNORE", "UTF-8", $multiBlockEnvInfo);
		Log::logger('multiBlockEnvInfo: ' . $multiBlockEnvInfo);
		$multiBlockEnvInfo = base64_encode($multiBlockEnvInfo);			
		$res = $this->httpClient->post($this->config['insertMultiBlockEnvInfo'], $multiBlockEnvInfo);
		Log::logger('BlockEnvInfo Response: ' . $res);			
		Log::logger('All BlockEnvInfo Uploaded!');
	}
}
?>